package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.starseas_phone.Adapters.Tea_apply_listviewAdapter;
import com.example.starseas_phone.Entity.Apply;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TeaApplyActivity extends AppCompatActivity {

    private List<Apply> list = new ArrayList<>();
    private Tea_apply_listviewAdapter adapter;
    private ListView tea_apply_list;
    private String CS = "测试TeaApplyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tea_apply);
        initid();
        Teainitview();
        tea_apply_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Apply apply = adapter.getItem(i);
                //老师进入学生信息修改详情
                if (apply.getApplicationType()==2){
                    if (apply.getStatus()==1){
                        Intent intent = new Intent(TeaApplyActivity.this,StuApply_detailsActivity.class);
                        intent.putExtra("id","3");
                        intent.putExtra("apply",apply);
                        startActivity(intent);
                    }else {
                        Toast.makeText(TeaApplyActivity.this,"该申请已经处理",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private void initid(){
        tea_apply_list = (ListView) findViewById(R.id.tea_apply_list);
        adapter = new Tea_apply_listviewAdapter(TeaApplyActivity.this,R.layout.tea_apply_listview,list);
    }

    private void Teainitview(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/TheApplicationForm/GetStudentApplicationFormByWorkerToken?token="+ Sto.teachers.getToken()+"&classId="+Sto.teachers.getClassId())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String message = jsonObject.getString("message");
                    Log.d(CS,message);
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        int id = jsonObject1.getInt("Id");
                        String ApplicantsId = jsonObject1.getString("ApplicantsId");
                        int ApplicationType = jsonObject1.getInt("ApplicationType");
                        int Status = jsonObject1.getInt("Status");
                        String ByTheTime = jsonObject1.getString("ByTheTime");
                        String InfoJson = jsonObject1.getString("InfoJson");
                        if (Status==1||Status==2||Status==3){
                            Apply tea_apply = new Apply(id,ApplicantsId,ApplicationType,Status,ByTheTime,InfoJson);
                            list.add(tea_apply);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            tea_apply_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onRestart() {
        list.clear();
        adapter.clear();
        Teainitview();
        super.onRestart();
    }
}